<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="商户ID" prop="machId">
        <el-input
          v-model="queryParams.machId"
          placeholder="请输入商户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="结算周期" prop="cycle">
        <el-input
          v-model="queryParams.cycle"
          placeholder="请输入结算周期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算金额" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入结算金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="账户ID" prop="bankId">
        <el-input
          v-model="queryParams.bankId"
          placeholder="请输入账户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:settle:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:settle:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:settle:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:settle:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="settleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="商户昵称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.provider.account}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算周期" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cycleStart, "{y}-{m}-{d}") }}-{{ parseTime(scope.row.cycleEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算金额" align="center" prop="price" />
      <el-table-column
        label="发起时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="danger" size="mini" v-if="scope.row.status == 0"
            >待审核</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.status == 1"
            >待打款</el-tag
          >
          <el-tag type="success" size="mini" v-if="scope.row.status == 2"
            >已打款</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:settle:edit']"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:settle:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleAccount(scope.row)"
            v-hasPermi="['system:settle:edit']"
            >银行账号</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="账号信息" :visible.sync="open2" append-to-body>
      <el-form label-width="80px" v-if="bankList.length>0">
        <el-card class="mt10" v-for="(item,index) in bankList" :key="index">
          <div slot="header" class="clearfix">
            <span>收款账户</span>
          </div>
          <div>
            <div class="item">
              <span>公司名称:{{ item.company }}</span>
            </div>
            <div class="item">
              <span>税号:{{ item.dutyNo }}</span>
            </div>
            <div class="item">
              <span>公司地址:{{ item.addr }}</span>
            </div>
            <div class="item">
              <span>开户银行:{{ item.openBank }}</span>
            </div>
            <div class="item">
              <span>银行账户:{{ item.bankNo }}</span>
            </div>
          </div>
        </el-card>
      </el-form>
      <el-empty v-else description="暂无数据" />
    </el-dialog>

    <!-- 添加或修改商户申请结算信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="商户ID" prop="machId">
          <el-input v-model="form.machId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="结算周期" prop="cycle">
          <el-input
            v-model="form.cycle"
            placeholder="请输入结算周期"
            disabled
          />
        </el-form-item>
        <el-form-item label="结算金额" prop="price">
          <el-input
            v-model="form.price"
            placeholder="请输入结算金额"
            disabled
          />
        </el-form-item>
        <el-form-item label="账户ID" prop="bankId">
          <el-input v-model="form.bankId" placeholder="请输入账户ID" />
        </el-form-item> -->
        <el-card class="">
          <div slot="header" class="clearfix">
            <span>商家信息</span>
          </div>
          <div class="item">
            <span>商家名称:{{ form3.account }}</span>
          </div>
          <div class="item">
            <span>结算周期:{{ parseTime(form.cycleStart, "{y}-{m}-{d}") }}-{{ parseTime(form.cycleEnd, "{y}-{m}-{d}") }}</span>
          </div>
          <div class="item">
            <span>结算金额:{{ form.price }}</span>
          </div>
        </el-card>
        <!-- <el-card class="mt10">
          <div slot="header" class="clearfix">
            <span>收款账户</span>
          </div>
          <div class="item">
            <span>公司名称:{{ form2.company }}</span>
          </div>
          <div class="item">
            <span>税号:{{ form2.dutyNo }}</span>
          </div>
          <div class="item">
            <span>公司地址:{{ form2.addr }}</span>
          </div>
          <div class="item">
            <span>开户银行:{{ form2.openBank }}</span>
          </div>
          <div class="item">
            <span>银行账户:{{ form2.bankNo }}</span>
          </div>
        </el-card> -->
        <el-button class="mt10" @click="orderView(form)">查看订单</el-button>
      <el-table
        v-loading="loading"
        :data="orderList"
        v-if="orderList.length > 0">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-table :data="scope.row.orderDetails" align="center">
              <el-table-column label="商品名称">
                <template slot-scope="{ row }">
                  <span>{{ row.name }}</span>
                </template>
              </el-table-column>
              <el-table-column label="价格">
                <template slot-scope="{ row }">
                  <span>{{ row.price }}</span>
                </template>
              </el-table-column>
              <el-table-column label="数量">
                <template slot-scope="{ row }">
                  <span>{{ row.buyCount }}</span>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="订单号" align="center" prop="orderCode" />
        <el-table-column
          label="用户ID"
          align="center"
          prop="userId"
          width="80"
        />
        <el-table-column label="服务时间段" align="center" prop="timePeriod" />
        <el-table-column label="支付状态" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" size="mini" v-if="scope.row.payStatus == 0"
              >未支付</el-tag
            >
            <el-tag type="success" size="mini" v-if="scope.row.payStatus == 1"
              >已支付</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="订单金额" align="center" prop="orderPrice" />
        <el-table-column label="订单类型" align="center">
          <template slot-scope="scope">
            <el-tag type="warning" size="mini" v-if="scope.row.orderType == 1"
              >家政</el-tag
            >
            <el-tag type="danger" size="mini" v-if="scope.row.orderType == 2"
              >开锁</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="商户ID" align="center" prop="merchId" />
        <el-table-column label="支付时间" align="center" prop="payTime" />
        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">
            <el-tag size="mini">{{
              orderStatus[scope.row.orderStatus]
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="售后服务" align="center">
          <template slot-scope="scope">
            <el-popover
              placement="bottom"
              title="售后服务"
              width="200"
              trigger="click"
              v-if="scope.row.aftersales != null"
            >
              <div>
                <p>售后问题：{{ scope.row.aftersales.description }}</p>
                <p v-if="scope.row.aftersales.accepPlan == 1">
                  解决方案：全额退款
                </p>
                <p v-if="scope.row.aftersales.accepPlan == 2">
                  解决方案：部分退款
                </p>
                <p v-if="scope.row.aftersales.accepPlan == 3">
                  解决方案：优惠券补偿
                </p>
                <p v-if="scope.row.aftersales.accepPlan == 4">
                  解决方案：拒绝退款
                </p>
              </div>
              <p class="desc_p" slot="reference">查看售后</p>
            </el-popover>
            <div v-else>无售后</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          <el-popover
            placement="bottom"
            title="备注"
            width="200"
            trigger="click"
            :content="scope.row.remark"
          >
            <p class="desc_p" slot="reference">{{ scope.row.remark }}</p>
          </el-popover>
        </template>
      </el-table-column> -->
      </el-table>
      <el-form-item label="审核结果" class="mt10" v-if="isShow==0">
          <el-radio-group v-model="form.status">
            <el-radio label="1">通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="确认打款" class="mt10" v-if="isShow==1">
          <el-radio-group v-model="form.status">
            <el-radio label="2">确认</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSettle,
  getSettle,
  delSettle,
  addSettle,
  updateSettle,
  settleDetail,
  getSettleOrder,
  listBank
} from "@/api/system/settle";

export default {
  name: "Settle",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户申请结算信息表格数据
      settleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        machId: null,
        cycle: null,
        price: null,
        bankId: null,
        status: null,
        orderStatus: null,
      },
      // 表单参数
      form: {},
      bankList:[],
      //订单详情
      orderList: [],
      form2: {},
      form3: {},
      isShow: 0,
      orderStatus: {
        0: "未支付",
        1: "待出发",
        2: "待商户确认",
        3: "待用户确认",
        4: "已完成",
        11: "已取消",
        22: "售后",
        33: "售后完成",
      },
      // 表单校验
      rules: {
        machId: [
          { required: true, message: "商户ID不能为空", trigger: "blur" },
        ],
        cycle: [
          { required: true, message: "结算周期不能为空", trigger: "blur" },
        ],
        price: [
          { required: true, message: "结算金额不能为空", trigger: "blur" },
        ],
        bankId: [
          { required: true, message: "账户ID不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "发起时间不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "状态  0待审核  1待打款 2已打款不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商户申请结算信息列表 */
    getList() {
      this.loading = true;
      listSettle(this.queryParams).then((response) => {
        this.settleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
      this.orderList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        machId: null,
        cycle: null,
        price: null,
        bankId: null,
        createTime: null,
        status: null,
        orderStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户申请结算信息";
    },
    orderView(item) {
      getSettleOrder(item.id).then((res) => {
        this.orderList = res.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSettle(id).then((response) => {
        this.form = response.data;
        this.form2 = response.data.providerBank;
        this.form3 = response.data.provider;
        this.form.status = response.data.status.toString();
        this.isShow = response.data.status;
        this.open = true;
        this.title = "审核商户申请结算信息";
      });
    },
    //银行账户
    handleAccount(row){
      this.reset();
      console.log(row);
      listBank({machId:row.machId}).then((res)=>{
        this.bankList = res.rows
        this.open2 = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSettle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSettle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商户申请结算信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSettle(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/settle/export",
        {
          ...this.queryParams,
        },
        `settle_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.item {
  margin-bottom: 18px;
}
</style>
